Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Java Variables

In Java, a variable is a data container that stores data values while a Java application is running. Each variable is given a data type that identifies the kind and number of values it may store. A variable is the name of the data's memory location.

Table Of Content
  • Java Variables
  • Local variables
  • Instance variables
  • Static variables of a class




Key Point on Variable

  • A memory location's name is specified as a variable. It is a program's fundamental storage unit name.
  • Variables can have their value modified while a programme is running.
  • A memory location only has a name; it is not a variable. Any actions performed on the variable have an impact on that memory location.
  • Before being used, all variables in Java must be defined.

How to declare variables?

We can declare variables in Java as shown below


How to initialize variables?

Three factors, which are as follows, involve in initialize a variables


  • Datatype- The kind of information that can be kept in this variable.
  • Variable Name- The variable's name is its variable name.
  • Value- This is the variable's starting point.

Example

 class Simple{  
public static void main(String args[ ]){
int x, y, z;   // Declares three ints, x, y, and z.  
int a=10, b=20, c=30;   // Example of initialization.  
double pi=3.14159;   // // declares and assigns a value of PI in varibel 'pi'  
}
}




Types of variables

In Java,we have 3 different variable explained below

  • Local variables
  • Instance variables
  • Static variables of a class
Local variables

The term "local variable" refers to a variable defined inside a block, method, or constructor.

  • When the block is entered, the function is called, or both, and these variables are generated, they are removed when the block is left.
  • For local variables, access modifiers are not permitted.
  • These variables are exclusively accessible within the block in which they were defined, meaning that only within that block do they have any scope.
  • Internally, local variables are implemented at the stack level.
  • The local variable must be initialised before being used inside the given scope.
 class Simple{  
public void DisplayScore() {
int score = 0; 
score = score + 24;
System.out.println("Your Score is : "+score );
}
public static void main(String args[ ]){
Simple simple = new Simple(); 
simple. DisplayScore(); 
 }
}



Output:

Your Score is : 24 

Instance Variables

  • Instance variables are produced when a class object is formed and deleted when the object is destroyed since they are defined in a class.
  • We may use access specifiers for instance variables, unlike local variables. The default access specifier will be applied if no access specifier is specified.
  • An instance variable does not need to be initialised. The default setting is 0.
  • Only by constructing objects can one access instance variables.
  • For instance variables, access modifiers are accessible.
 import java.io.*;
 public class Student{  
public String name;  // instance variable is visible for any child class
private  int roll;  // private variable is only visible to class 
public Student (String stuName){
name =stuName;
 }
// The roll variable is assigned a value
public void setRoll (int stuRoll){
roll =stuRoll;
 }
// This method prints the Student details.
public void printStudent() {
System.out.println("Name : "+ name);
System.out.println("Roll : "+ roll);
 }
public static void main(String args[ ]){
Student student = new Student("Alok"); 
student. setRoll(99); 
student. printStudent(); 
 }
}



Output:

Name : Alok 
Roll : 99 

Class/Static Variables

Static variables are also known as class variables.


  • Similar to instance variables, these variables are also specified as such. The difference is that static variables are declared using the static keyword within a class outside of any method, method, constructor or block.
  • No matter how many objects we create, we can only have one instance of a static variable per class, in contrast to instance variables.
  • Static variables are generated at programme execution's beginning and immediately deleted at its conclusion.
  • A static variable does not need to be initialised. Its initial value is 0.
  • The compiler will display a warning message, but the programme won't crash, if we attempt to access a static variable like an instance variable (via an object). The object name and class name are automatically swapped out by the compiler.
 import java.io.*;
 public class Student{  
private static double roll;  // roll variable is a private static variable
public static final String college="Patliputra University"; // college  is a constant 
public static void main(String args[ ]){
roll=100; 
System.out.println("Roll : "+ roll); // here no need to create object because of static keyword
System.out.println("College : "+ college);
 }
}



Output:

Roll : 100 
College : Patliputra University 
Java Data Types & Unicode System Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Naming Convention
Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.